import html2canvas from "html2canvas";

//DOM转为Canvas
export const domToCanvas = async (dom: HTMLElement) => {
  return html2canvas(dom, {
    useCORS: true,
    allowTaint: false,
    backgroundColor: "transparent", // 设置背景为透明
  });
};

export async function generateAndDownloadImage(dom: HTMLElement) {
  try {
    const canvas = await domToCanvas(dom);

    // 将Canvas转换为图片并下载
    const dataURL = canvas.toDataURL("image/png");
    const link = document.createElement("a");
    link.href = dataURL;
    link.download = `${Date.now()}.png`; // 使用时间戳作为文件名
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  } catch (error) {
    console.error("生成图片失败：", error);
  }
}
